import requests


"""
    员工增删改查接口封装层
"""
class IhrmEmpCrud:

    # 添加员工接口封装
    @classmethod
    def add_emp(cls,json_data,token):
        add_emp_url = "https://ihrm-java.itheima.net/api/sys/user"
        header = {"Authorization":token}
        resp = requests.post(url=add_emp_url,headers=header,json=json_data)
        return resp
    # 查询员工列表
    @classmethod
    def query_emp_list(cls, token):
        query_emp_url = "https://ihrm-java.itheima.net/api/sys/user?page=1&size=10"
        header = {"Authorization": token}
        resp = requests.get(url=query_emp_url, headers=header)
        return resp
    # 查询单个员工信息
    @classmethod
    def query_emp(cls, token,emp_id):
        query_emp_url = "https://ihrm-java.itheima.net/api/sys/user/"+emp_id
        print(query_emp_url)
        header = {"Authorization": token}
        resp = requests.get(url=query_emp_url, headers=header)
        return resp
    # 修改员工信息
    @classmethod
    def update_emp(cls, json_data, token, emp_id):
        update_emp_url = "https://ihrm-java.itheima.net/api/sys/user/" + emp_id
        header = {"Authorization": token}
        resp = requests.put(url=update_emp_url, headers=header, json=json_data)
        return resp
    # 删除员工信息
    @classmethod
    def delete_emp(cls, token, emp_id):
        delete_emp_url = "https://ihrm-java.itheima.net/api/sys/user/" + emp_id
        header = {"Authorization": token}
        resp = requests.delete(url=delete_emp_url, headers=header)
        return resp

if __name__ == '__main__':
    # 获取token
    data = {"mobile": "13800000002", "password": "929itheima.CN032@.20250615"}
    login_url = "https://ihrm-java.itheima.net/api/sys/login"
    login_header = {"Content-Type": "application/json"}
    token =requests.post(url=login_url,headers=login_header,json=data).json().get('data')
    # 添加员工
    json_data = {
    "username": "大猪乔治",
    "mobile": "13899834431",
    "timeOfEntry": "2021-12-01",
    "formOfEmployment": 1,
    "workNumber": "777888",
    "departmentName": "测试",
    "departmentId": "1452603344685203456",
    "correctionTime": "2021-12-30T16:00:00.000Z"
    }
    IhrmEmpCrud.add_emp(json_data, token)
    # 查询员工列表
    resp1 = IhrmEmpCrud.query_emp_list(token)
    # print("查询全部员工信息：", resp1.json())
    # 查询员工个人信息
    resp2 = IhrmEmpCrud.query_emp(token,"1063705989926227968")
    print("查询员工信息：",resp2.json())
    # 修改员工信息
    update_emp_data = {"username":"齐天大圣"}
    IhrmEmpCrud.update_emp(json_data=update_emp_data,token=token,emp_id="1063705989926227968")


